package com.mars.weixin.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.mars.boss.weixin.entity.WxArticle;
import com.mars.boss.weixin.entity.WxKeyWord;
import com.mars.boss.weixin.entity.WxMenu;
import com.mars.boss.weixin.entity.WxText;
import com.mars.boss.weixin.service.WxArticleGroupService;
import com.mars.boss.weixin.service.WxArticleService;
import com.mars.boss.weixin.service.WxKeyWordService;
import com.mars.boss.weixin.service.WxMenuService;
import com.mars.boss.weixin.service.WxTextService;
import com.mars.weixin.common.WeixinConstant;
import com.mars.weixin.model.msg.Article;
import com.mars.weixin.model.msg.BaseMsg;
import com.mars.weixin.model.msg.NewsMessage;
import com.mars.weixin.model.msg.TextMessage;
import com.mars.weixin.service.MsgService;

@Service
public class MsgServiceImpl implements MsgService {

	@Autowired
	private WxMenuService menuService;

	@Autowired
	private WxKeyWordService keyWordService;

	@Autowired
	private WxTextService textService;

	@Autowired
	private WxArticleService articleService;

	@Autowired
	private WxArticleGroupService articleGroupService;

	@Override
	public Object clickEventHandler(BaseMsg baseMsg) {
		String eventKey = baseMsg.getEventKey().trim();

		WxMenu menu = this.menuService.selectMenyByKey(eventKey);
		if (null != menu) {
			return getData(baseMsg, menu.getTemplateType(), menu.getTemplateId());
		}
		return null;
	}

	@Override
	public Object textKeyWordHandler(BaseMsg baseMsg) {
		String content = baseMsg.getContent().trim();

		WxKeyWord keyword = this.keyWordService.selectByKeyWord(content);
		if (null != keyword && !"".equals(keyword.getKeyword().trim())) {
			return getData(baseMsg, keyword.getTemplateType(), keyword.getTemplateId());
		}

		return null;
	}

	public Object getData(BaseMsg baseMsg, String templateType, Long templateId) {
		if ("text".equals(templateType)) {
			WxText text = this.textService.selectById(templateId);

			TextMessage textMessage = new TextMessage();
			textMessage.setToUserName(baseMsg.getFromUserName());
			textMessage.setFromUserName(baseMsg.getToUserName());
			textMessage.setCreateTime(new Date().getTime());
			textMessage.setMsgType(WeixinConstant.RESP_MESSAGE_TYPE_TEXT);
			textMessage.setContent(text.getContent());

			return textMessage;
		} else if ("article_single".equals(templateType)) {
			WxArticle wxArticle = this.articleService.selectById(templateId);
			NewsMessage newsMessage = new NewsMessage();

			newsMessage.setToUserName(baseMsg.getFromUserName());
			newsMessage.setFromUserName(baseMsg.getToUserName());
			newsMessage.setCreateTime(new Date().getTime());
			newsMessage.setMsgType(WeixinConstant.RESP_MESSAGE_TYPE_NEWS);
			newsMessage.setArticleCount(1);

			List<Article> articles = new ArrayList<Article>();

			Article item = new Article();
			item.setTitle(wxArticle.getTitle());
			item.setPicUrl(wxArticle.getPicUrl());
			item.setUrl(wxArticle.getUrl());
			item.setDescription(wxArticle.getDescription());

			articles.add(item);
			newsMessage.setArticles(articles);

			return newsMessage;
		} else if ("article_group".equals(templateType)) {
			List<WxArticle> list = this.articleGroupService.getArticles(templateId);

			NewsMessage newsMessage = new NewsMessage();

			newsMessage.setToUserName(baseMsg.getFromUserName());
			newsMessage.setFromUserName(baseMsg.getToUserName());
			newsMessage.setCreateTime(new Date().getTime());
			newsMessage.setMsgType(WeixinConstant.RESP_MESSAGE_TYPE_NEWS);
			newsMessage.setArticleCount(list.size());

			List<Article> articles = new ArrayList<Article>();

			for (WxArticle article : list) {
				Article item = new Article();
				item.setTitle(article.getTitle());
				item.setPicUrl(article.getPicUrl());
				item.setUrl(article.getUrl());
				item.setDescription(article.getDescription());

				articles.add(item);
			}

			newsMessage.setArticles(articles);

			return newsMessage;
		}
		return null;
	}
}
